// pages/car_info/car_info.js
import { Config } from '../../utils/config.js';
import { Img } from '../../utils/img.js';
var config_img = new Img();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_type: '3',
    base_url: Config.imgUrl,
    img31: '',
    img32: '',
    show_sl1:true,
    show_sl2: true,
    operate1: "点击上传",
    operate2: "点击上传",
    navigatenumber:'1'
  },

  uploadImage: function (e) {
    var that = this;
    wx.showActionSheet({
      itemList: ['拍照', '从相册中选择'],
      success: function (res) {
        that.SelectImage(res.tapIndex, e);
      },
    })
  },

  //
  SelectImage: function (tapIndex, typeid) {
    var that = this;
    if (tapIndex == '0')
      var sourceType = ['camera'];
    else
      var sourceType = ['album'];
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: sourceType,
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        that.SaveImage(res.tempFilePaths[0], typeid)
      }
    })
  },

  //保存图片
  SaveImage: function (img, typeid) {
    var that = this;
    var id = ''
    if (typeid.target.dataset.id)
      id = typeid.target.dataset.id;
     var index = typeid.target.dataset.index;
    var data = { img: img, type: typeid.target.dataset.type, id: id, img_type: that.data.img_type };
    config_img.SaveImage(data, data => {
      if (data.code == 201) {
        that.setData({
          navigatenumber: '2',
        })
        var imginfo = 'img' + typeid.target.dataset.type;
        if (index == 1) {
          that.setData({
            [imginfo]: img,
            show_sl1: false,
            operate1: "重新上传"
          })
        } else {
          that.setData({
            [imginfo]: img,
            show_sl2: false,
            operate2: "重新上传"
          })
        }
        //提示成功 跳转
        wx.showToast({
          title: '上传成功',
          icon: 'success',// 'none'为无图标
          duration: 2000,//多少毫秒后消失      
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var data = { img_type: that.data.img_type };
    config_img.GetImageInfo(data, data => {
      if (data != "") {
        var navigatenumber = '1';
        if (options.navigatenumber)
          var navigatenumber = options.navigatenumber;
        that.setData({
          img_info: data,
          show_sl2: false,
          show_sl1: false,
          operate1: "重新上传",
          operate2: "重新上传",
          navigatenumber: navigatenumber,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var number = this.data.navigatenumber;
    if (number > 1)
      wx.navigateBack({
        delta: number
      })
  },
})